package com.example.smart_city_0829.activity.volunteering;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.Base;
import com.example.smart_city_0829.entity.volunteering.VolunteeringList;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;

public class VolunteeringActivity extends BaseActivity {

    private EditText searchEt;
    private ListView lv;
    private Button loadMoreBt;


    private int pageNum = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_volunteering;
    }

    @Override
    protected void initView() {
        searchEt = (EditText) findViewById(R.id.search_et);
        lv = (ListView) findViewById(R.id.lv);
        loadMoreBt = (Button) findViewById(R.id.load_more_bt);

    }

    @Override
    protected void initData() {
        getActivityList();
    }

    private LvAdapter lvAdapter;
    private List<VolunteeringList.RowsBean> data = new ArrayList<>();

    private void getActivityList() {
        api.getVolunteeringList(Constant.pageSize, pageNum).enqueue(new NetCallBack<VolunteeringList>() {
            @Override
            public void onResponse(Call<VolunteeringList> call, Response<VolunteeringList> response) {
                if (response.body() == null) return;
                final List<VolunteeringList.RowsBean> rows = response.body().getRows();
                if (lvAdapter == null) {
                    data = rows;
                    lvAdapter = new LvAdapter(data, context);
                    lv.setAdapter(lvAdapter);
                }else {
                    if (pageNum == 1){
                        data = rows;
                    }else {
                        data.addAll(rows);
                    }
                    lvAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        loadMoreBt.setOnClickListener((v) -> {
            pageNum++;
            getActivityList();
        });

        //TODO 搜索活动，未找到api
        searchEt.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                pageNum = 1;
                final String key = searchEt.getText().toString();

                return true;
            }
            return false;
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final Intent intent = new Intent(context, VolunteeringDetailActivity.class);
                final VolunteeringList.RowsBean item = (VolunteeringList.RowsBean) parent.getAdapter().getItem(position);
                intent.putExtra("data", gson.toJson(item));
                startActivity(intent);
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "志愿服务";
    }

    class LvAdapter extends CommonAdapter<VolunteeringList.RowsBean, CommonAdapter.CommListViewHolder2> {


        public LvAdapter(List<VolunteeringList.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(VolunteeringList.RowsBean data, int position, CommListViewHolder2 viewHolder, View convertView, Context context) {
            viewHolder.imageView.setVisibility(View.GONE);
            viewHolder.name.setText(String.valueOf(data.getTitle()));
            viewHolder.workYearsTv.setMaxLines(1);
            viewHolder.workYearsTv.setEllipsize(TextUtils.TruncateAt.END);
            viewHolder.workYearsTv.setText(String.valueOf(data.getUndertaker()));
            viewHolder.goodAtTv.setText("人员要求：" + data.getRequireText());
            viewHolder.serviceTimesTv.setText("开始时间：");
            viewHolder.favorableRateTv.setText(data.getStartAt());
            viewHolder.quesBt.setText("报名");
            //报名按钮
            viewHolder.quesBt.setOnClickListener((v) -> {
                final HashMap<String, Object> params = new HashMap<>();
                params.put("activityId",data.getId());
                params.put("newState","true");
                api.postVolunteeringActivity(
                        RequestBody.create(gson.toJson(params), MediaType.parse(Constant.MediaTypeParseJsonValue))
                ).enqueue(new NetCallBack<Base>() {
                    @Override
                    public void onResponse(Call<Base> call, Response<Base> response) {
                        if (response.body() == null) return;
                        if (response.body().getCode() == 200) {
                            showToastShort("报名成功");
                        }else {
                            showToastShort("报名失败");
                        }
                    }
                });
            });
        }

        @Override
        protected CommListViewHolder2 getViewHolder() {
            return new CommListViewHolder2();
        }

        @Override
        protected void initView(CommListViewHolder2 viewHolder, View convertView) {
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.workYearsTv = (TextView) convertView.findViewById(R.id.work_years_tv);
            viewHolder.goodAtTv = (TextView) convertView.findViewById(R.id.goodAt_tv);
            viewHolder.serviceTimesTv = (TextView) convertView.findViewById(R.id.serviceTimes_tv);
            viewHolder.favorableRateTv = (TextView) convertView.findViewById(R.id.favorableRate_tv);
            viewHolder.quesBt = (Button) convertView.findViewById(R.id.ques_bt);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.lawyer_list_item;
        }
    }

}